__author__ = 'Soul'

from core.PageFetcher import *
import string

class C360AppFetcher(PageFetcher):
     def init(self, url, intent=False, recurse=False):
        return PageFetcher.init(self, url, True)

     def analysis(self):
        if self.__html_src__:
            global seen_urls, urls_to_visit
            from lxml.html import fromstring
            parsed_html = fromstring(self.__html_src__)
            search_ul_lis = parsed_html.xpath("//div[@class='SeaCon']/ul/li")

            for app_li in search_ul_lis:
                app_link = app_li.xpath("dl/dd/h3/a/@href")
                if type(app_link) == list:
                    app_link = app_link[0]
                app_link = "http://zhushou.360.cn" + app_link.decode('utf-8')
                urls_to_visit.append({"url": app_link, "controller": "C360AppPageFetcher"})

            current_page = 0
            current_page_start = self.__html_src__.find('pg.page=')
            if current_page_start != -1:
                current_page_end = self.__html_src__.find(';', current_page_start)
                current_page_line = self.__html_src__[current_page_start: current_page_end]
                current_page = string.atoi(current_page_line.split("=")[1])

            total_page = current_page
            total_page_start = self.__html_src__.find('pg.pageCount = parseInt(\'')
            if total_page_start != -1:
                #  pg.pageCount = parseInt('8',10)||0;
                total_page_end = self.__html_src__.find(',', total_page_start + 1)
                total_page_line = self.__html_src__[total_page_start: total_page_end - 1]
                tmpidx = total_page_line.find('\'')
                total_page = total_page_line[tmpidx+1:]
                total_page = string.atoi(total_page)

                # get the next page URL
                url_str = self.__url__
                items = url_str.split('&')

                pn = string.atoi((items[1]).split('=')[1])
                if pn < total_page:
                    next_pn = pn + 1
                    next_url = "%s&page=%s" %(items[0], str(next_pn))
                    urls_to_visit.append(next_url)
